package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.PartBaseInfo;
import com.sl.au.entity.BusinesssellerBaseInfo;
import com.sl.au.entity.BussinessLevel;
import com.sl.au.entity.Division;
import com.sl.au.entity.GridAreaInfo;
import com.sl.au.entity.Option;
import com.sl.au.service.DivisionService;
import com.sl.au.service.GridAreaInfoService;
import com.sl.au.service.IdentifyService;
import com.sl.au.service.OptionService;
import com.sl.au.service.PartBaseInfoService;
import com.sl.utils.MsgHelper;

/**
 * 部件绑定权属部门
 * 
 * @author yangdi
 *
 */
@RestController
@RequestMapping(value = "/ui/bindingGridArea")
public class BindingGridAreaController extends BaseController {

	@Autowired
	GridAreaInfoService gridAreaInfoService;
	
	@Autowired
	DivisionService divisionService; 
	
	@Autowired
	PartBaseInfoService partBaseInfoService;
	
	@Autowired
	OptionService optionService;
	
	@Autowired
	IdentifyService identifyService;
	
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(){
		ModelAndView modelAndView = this.createLayoutView("partBaseInfo/bindingGridArea");
		List<Option> options = optionService.getRepository().findByGroup("部件状态");
		modelAndView.addObject("options", options);
		return modelAndView;
	}
	
	/**
	 * 获取部件区域树信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="getAllNodes",method = RequestMethod.GET)
	public Map<String,Object> getAllNodes(HttpServletRequest request)
	{
		Map<String,Object> map = new HashMap<String,Object>();
		List<Division> node = null;
		
		node = divisionService.getAllNodes();
		List<Map<String,Object>> nodeMap=new ArrayList<Map<String,Object>>();
		nodeMap=divisionService.getNodeMap(node);
		
		map.put("code", 1);
		map.put("nodes", nodeMap);
	
		return map;
	}
	
	/**
	 * 获取绑定区域网格的信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getGridAreaPage", method = RequestMethod.POST)
	public Map<String,Object> getGridAreaPage(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String divisionId=request.getParameter("divisionId");
		List<GridAreaInfo> modules=null;
		int count = 0;
		
		Division division = divisionService.getRepository().findOne(divisionId);
		modules = gridAreaInfoService.getPageByDivision(start, length, division, null, null);
		count = gridAreaInfoService.getCountByDivision(division);
		
		map.put("data", modules);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	/**
	 * 获取绑定网格部件的信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getPartPageByGrid", method = RequestMethod.POST)
	public Map<String,Object> getPartPageByGrid(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		String gridId=request.getParameter("gridId");
		List<PartBaseInfo> modules=null;
		int count = 0;
		GridAreaInfo gridAreaInfo = null;
		if(!"isNull".equals(gridId)) {
			gridAreaInfo = gridAreaInfoService.getRepository().findOne(gridId);
			if(gridAreaInfo == null) {
				gridAreaInfo = new GridAreaInfo();
			}
		}
		modules = partBaseInfoService.getPageByGridAreaInfoAndKey(start, length, gridAreaInfo, key, "createTime", Sort.Direction.DESC);
		count = partBaseInfoService.getCountByGridAreaInfoAndKey(gridAreaInfo, key);
		
		map.put("data", modules);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	
	/**
	 * 绑定区域层级关系
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/addData",method = RequestMethod.POST)
	public MsgHelper bindingGridAreaInfo(@RequestBody Map<String,Object> map){
		MsgHelper msgHelper = MsgHelper.success();
		String id = (String) map.get("id");
		String gridId = String.valueOf(map.get("gridId"));
		
		PartBaseInfo partBaseInfo = partBaseInfoService.getRepository().findOne(id);
		GridAreaInfo gridAreaInfo = gridAreaInfoService.getRepository().findOne(gridId);

		partBaseInfo.setGridAreaInfo(gridAreaInfo);
		
		
		if(gridAreaInfo.getDivision() != null) {
			String divisionIdentify = identifyService.getIdentifyByType(gridAreaInfo.getDivision().getId(), "gridShow", "division");
			partBaseInfo.setDivisionIdentify(divisionIdentify);
		}else {
			partBaseInfo.setDivisionIdentify(null);
		}
		
		partBaseInfoService.getRepository().save(partBaseInfo);
		msgHelper.setMsg("网格绑定部件信息成功!");
		return msgHelper;
	};
	
	/**
	 * 解除绑定区域层级管理
	 * @param id
	 * @return
	 */
	@RequestMapping("/removeData/{id}")
	public MsgHelper unbindGridAreaInfo(@PathVariable("id") String id) {
		MsgHelper msgHelper = MsgHelper.success();
		PartBaseInfo partBaseInfo = partBaseInfoService.getRepository().findOne(id);
		
		partBaseInfo.setGridAreaInfo(null);
		partBaseInfo.setDivisionIdentify(null);
		
		partBaseInfoService.getRepository().save(partBaseInfo);
		msgHelper.setMsg("网格移除部件成功!");
		return msgHelper;
	}
	
	
	@RequestMapping(value="/addDataAll",method = RequestMethod.POST)
	public Map<String,Object> addDataAll(@RequestBody Map<String,Object> map){
		Map<String,Object> msg = new HashMap<String,Object>();
		String gridId = (String) map.get("gridId");
		ArrayList<String> dataInfo = (ArrayList<String>) map.get("dataInfo");
		GridAreaInfo gridAreaInfo = gridAreaInfoService.getRepository().findOne(gridId);
		String divisionIdentify = null;
		if(gridAreaInfo.getDivision() != null) {
			divisionIdentify = identifyService.getIdentifyByType(gridAreaInfo.getDivision().getId(), "gridShow", "division");
		}
		for(String partBaseInfoId : dataInfo) {
			  PartBaseInfo partBaseInfo = partBaseInfoService.getRepository().findOne(partBaseInfoId);
			  partBaseInfo.setGridAreaInfo(gridAreaInfo);
			  partBaseInfo.setDivisionIdentify(divisionIdentify);
			  partBaseInfoService.getRepository().save(partBaseInfo);
		}
		msg.put("msg", "网格绑定部件信息成功!");
		return msg;
	};
	
	@RequestMapping(value="/removeDataAll",method = RequestMethod.POST)
	public Map<String,Object> removeDataAll(@RequestBody Map<String,Object> map){
		Map<String,Object> msg = new HashMap<String,Object>();
		ArrayList<String> dataInfo = (ArrayList<String>) map.get("dataInfo");
		for(String partBaseInfoId : dataInfo) {
			PartBaseInfo partBaseInfo = partBaseInfoService.getRepository().findOne(partBaseInfoId);
			partBaseInfo.setGridAreaInfo(null);
			partBaseInfo.setDivisionIdentify(null);
			partBaseInfoService.getRepository().save(partBaseInfo);
		}
		msg.put("msg", "网格移除部件成功!");
		return msg;
	};
	
}
